package com.yycx.module.bus.provider.handler.haiying.order;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 海鹰订单回款商品列表扩展
 *
 * @author zyf
 */
@Component("hyOrderReturnHandler")
public class HyOrderReturnHandler implements PageInterceptor<HyOrderDetail> {



    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrderDetail> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap map : result) {
            Long orderDetailsId = map.getLong("orderDetailsId");
            Integer customBondType = map.getInt("customBondType");
            BigDecimal saleAmount = map.getBigDecimal("saleAmount");
            BigDecimal saleNum = map.getBigDecimal("saleNum");
            BigDecimal returnAmount = map.getBigDecimal("returnAmount");
            BigDecimal returnNum = map.getBigDecimal("saleNum");
            BigDecimal customBond = map.getBigDecimal("customBond");
            BigDecimal customOverBond = map.getBigDecimal("customOverBond");
            //预付款情况计算未回款金额
            if (customBondType.equals(7)) {
                BigDecimal customYsk = map.getBigDecimal("customYsk");
                if (FlymeUtils.isNotEmpty(customYsk)) {
                    map.put("customYsk", customYsk);
                    //计算未回款金额
                    map.put("noReturnAmount", saleAmount.subtract(customYsk).subtract(returnAmount));
                } else {
                    map.put("returnAmount", "金额异常");
                }
            } else {
                //资信或者全款
                if (FlymeUtils.isNotEmpty(returnAmount)) {
                    map.put("returnAmount", returnAmount);
                    //计算未回款金额
                    map.put("noReturnAmount", saleAmount.subtract(returnAmount));
                }
            }
            map.put("customBondAmount", customBond.add(customOverBond));
        }
        return result;
    }
}
